package com.example.mydy;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.adapter.PrivilegeUserAdapter;
import com.example.mydy.model.UserInfo;
import com.example.mydy.utils.SharedPreferencesManager;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

public class TaskRewardActivity extends AppCompatActivity {
    private static final String TAG = "TaskRewardActivity";
    private static final String PREF_NAME = "TaskRewardPrefs";
    
    // 任务奖励设置
    private Switch taskRewardSwitch;
    private EditText taskRewardAmount;
    private EditText taskLevel1Percentage;
    private EditText taskLevel2Percentage;
    private EditText taskLevel3Percentage;
    
    // 邀请奖励设置
    private Switch inviteRewardSwitch;
    private EditText inviteRewardAmount;
    
    // 邀请奖励分成比例设置
    private EditText inviteLevel1Percentage;
    private EditText inviteLevel2Percentage;
    private EditText inviteLevel3Percentage;
    
    private RecyclerView privilegeUserList;
    private PrivilegeUserAdapter privilegeUserAdapter;
    private List<UserInfo> privilegeUsers;
    
    private FloatingActionButton fabAddPrivilege;
    
    private SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_reward_management);
        
        prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
        
        initViews();
        setupListeners();
        loadSettings();
        setupRecyclerView();
    }

    private void initViews() {
        // 任务奖励设置
        taskRewardSwitch = findViewById(R.id.taskRewardSwitch);
        taskRewardAmount = findViewById(R.id.taskRewardAmount);
        taskLevel1Percentage = findViewById(R.id.taskLevel1Percentage);
        taskLevel2Percentage = findViewById(R.id.taskLevel2Percentage);
        taskLevel3Percentage = findViewById(R.id.taskLevel3Percentage);
        
        // 邀请奖励设置
        inviteRewardSwitch = findViewById(R.id.inviteRewardSwitch);
        inviteRewardAmount = findViewById(R.id.inviteRewardAmount);
        
        // 邀请奖励分成比例设置
        inviteLevel1Percentage = findViewById(R.id.inviteLevel1Percentage);
        inviteLevel2Percentage = findViewById(R.id.inviteLevel2Percentage);
        inviteLevel3Percentage = findViewById(R.id.inviteLevel3Percentage);
        
        // 特权用户列表
        privilegeUserList = findViewById(R.id.recyclerViewPrivileges);
        
        // 添加特权用户按钮
        fabAddPrivilege = findViewById(R.id.fabAddPrivilege);
    }

    private void setupListeners() {
        // 任务奖励开关监听
        if (taskRewardSwitch != null) {
        taskRewardSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            updateTaskRewardEnabled(isChecked);
            if (privilegeUserAdapter != null) {
                privilegeUserAdapter.setTaskRewardEnabled(isChecked);
            }
            saveSettings();
        });
        }
        
        // 邀请奖励开关监听
        if (inviteRewardSwitch != null) {
        inviteRewardSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            updateInviteRewardEnabled(isChecked);
            if (privilegeUserAdapter != null) {
                privilegeUserAdapter.setInviteRewardEnabled(isChecked);
            }
            saveSettings();
        });
        }
        
        // 保存按钮监听
        Button saveButton = findViewById(R.id.saveSettingsButton);
        if (saveButton != null) {
            saveButton.setOnClickListener(v -> saveSettings());
        }
        
        // 添加特权用户按钮监听
        if (fabAddPrivilege != null) {
            fabAddPrivilege.setOnClickListener(v -> showAddPrivilegeUserDialog());
        }
    }

    private void setupRecyclerView() {
        // 设置特权用户列表
        privilegeUsers = new ArrayList<>();
        privilegeUserAdapter = new PrivilegeUserAdapter(privilegeUsers, this::removePrivilegeUser);
        
        // 设置全局奖励开关状态
        privilegeUserAdapter.setTaskRewardEnabled(taskRewardSwitch.isChecked());
        privilegeUserAdapter.setInviteRewardEnabled(inviteRewardSwitch.isChecked());
        
        privilegeUserList.setLayoutManager(new LinearLayoutManager(this));
        privilegeUserList.setAdapter(privilegeUserAdapter);
        
        // 加载特权用户数据
        loadPrivilegeUsers();
    }

    private void loadSettings() {
        loadMockSettings();
    }

    private void loadMockSettings() {
        // 从SharedPreferences加载设置
        taskRewardSwitch.setChecked(prefs.getBoolean("task_reward_enabled", true));
        taskRewardAmount.setText(String.valueOf(prefs.getInt("task_reward_amount", 100)));
        taskLevel1Percentage.setText(String.valueOf(prefs.getInt("task_level1_percentage", 10)));
        taskLevel2Percentage.setText(String.valueOf(prefs.getInt("task_level2_percentage", 5)));
        taskLevel3Percentage.setText(String.valueOf(prefs.getInt("task_level3_percentage", 2)));
        
        inviteRewardSwitch.setChecked(prefs.getBoolean("invite_reward_enabled", true));
        inviteRewardAmount.setText(String.valueOf(prefs.getInt("invite_reward_amount", 2)));
        
        // 加载邀请奖励分成比例设置
        inviteLevel1Percentage.setText(String.valueOf(prefs.getInt("invite_level1_percentage", 30)));
        inviteLevel2Percentage.setText(String.valueOf(prefs.getInt("invite_level2_percentage", 20)));
        inviteLevel3Percentage.setText(String.valueOf(prefs.getInt("invite_level3_percentage", 10)));
        
        updateTaskRewardEnabled(taskRewardSwitch.isChecked());
        updateInviteRewardEnabled(inviteRewardSwitch.isChecked());
    }

    private void loadPrivilegeUsers() {
        // 加载特权用户数据
        int count = prefs.getInt("privilege_user_count", 0);
        
        // 使用JSON格式加载每个用户的数据
        com.google.gson.Gson gson = new com.google.gson.Gson();
        privilegeUsers.clear();
        
        for (int i = 0; i < count; i++) {
            String userJson = prefs.getString("privilege_user_" + i, null);
            if (userJson != null) {
                try {
                    UserInfo user = gson.fromJson(userJson, UserInfo.class);
                    privilegeUsers.add(user);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
        // 如果没有保存的用户，则创建默认用户
        if (privilegeUsers.isEmpty()) {
            // 创建一些示例用户
            createDefaultPrivilegeUsers();
        }
        
        // 刷新适配器
        privilegeUserAdapter.notifyDataSetChanged();
    }
    
    private void createDefaultPrivilegeUsers() {
        // 创建默认用户示例
        UserInfo user1 = new UserInfo();
        user1.setUsername("特权用户1");
        user1.setId(1);
        
        UserInfo user2 = new UserInfo();
        user2.setUsername("VIP会员");
        user2.setId(2);
        
        // 添加到列表
        privilegeUsers.add(user1);
        privilegeUsers.add(user2);
    }
    
    private void showAddPrivilegeUserDialog() {
        // 创建对话框
        androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(this);
        
        // 使用布局文件中已有的NestedScrollView
        android.view.LayoutInflater inflater = getLayoutInflater();
        android.view.View dialogView = inflater.inflate(R.layout.dialog_add_user_privilege, null);
        
        // 设置对话框标题
        builder.setView(dialogView)
               .setTitle("添加用户特权奖励");
        
        // 查找对话框中的控件
        EditText etUsername = dialogView.findViewById(R.id.et_username);
        EditText etLevel1Ratio = dialogView.findViewById(R.id.et_level1_ratio);
        EditText etLevel2Ratio = dialogView.findViewById(R.id.et_level2_ratio);
        EditText etLevel3Ratio = dialogView.findViewById(R.id.et_level3_ratio);
        EditText etInviteReward = dialogView.findViewById(R.id.et_invite_reward);
        EditText etInviteLevel1Ratio = dialogView.findViewById(R.id.et_invite_level1_ratio);
        EditText etInviteLevel2Ratio = dialogView.findViewById(R.id.et_invite_level2_ratio);
        EditText etInviteLevel3Ratio = dialogView.findViewById(R.id.et_invite_level3_ratio);
        
        // 根据全局设置控制任务和邀请奖励控件的状态
        View taskRewardSection = dialogView.findViewById(R.id.task_reward_section);
        View inviteRewardSection = dialogView.findViewById(R.id.invite_reward_section);
        
        // 设置任务奖励和邀请奖励区域的可见性
        boolean taskRewardEnabled = taskRewardSwitch.isChecked();
        boolean inviteRewardEnabled = inviteRewardSwitch.isChecked();
        
        if (taskRewardSection != null) {
            taskRewardSection.setVisibility(taskRewardEnabled ? View.VISIBLE : View.GONE);
        }
        
        if (inviteRewardSection != null) {
            inviteRewardSection.setVisibility(inviteRewardEnabled ? View.VISIBLE : View.GONE);
        }
        
        // 预填充任务奖励比例
        etLevel1Ratio.setText(taskLevel1Percentage.getText().toString());
        etLevel2Ratio.setText(taskLevel2Percentage.getText().toString());
        etLevel3Ratio.setText(taskLevel3Percentage.getText().toString());
        
        // 预填充邀请奖励金额和分成比例
        etInviteReward.setText(inviteRewardAmount.getText().toString());
        etInviteLevel1Ratio.setText(inviteLevel1Percentage.getText().toString());
        etInviteLevel2Ratio.setText(inviteLevel2Percentage.getText().toString());
        etInviteLevel3Ratio.setText(inviteLevel3Percentage.getText().toString());
        
        Button btnStartDate = dialogView.findViewById(R.id.btn_start_date);
        Button btnEndDate = dialogView.findViewById(R.id.btn_end_date);
        CheckBox cbNoEndDate = dialogView.findViewById(R.id.cb_no_end_date);
        EditText etRemark = dialogView.findViewById(R.id.et_remark);
        Button btnSave = dialogView.findViewById(R.id.btn_save);
        Button btnCancel = dialogView.findViewById(R.id.btn_cancel);
        
        // 设置日期选择
        final String[] startDate = {""};
        final String[] endDate = {""};
        
        btnStartDate.setOnClickListener(v -> {
            // 显示日期选择器
            android.app.DatePickerDialog datePickerDialog = new android.app.DatePickerDialog(
                    TaskRewardActivity.this,
                    (view, year, month, dayOfMonth) -> {
                        startDate[0] = String.format("%d-%02d-%02d", year, month + 1, dayOfMonth);
                        btnStartDate.setText(startDate[0]);
                    },
                    java.util.Calendar.getInstance().get(java.util.Calendar.YEAR),
                    java.util.Calendar.getInstance().get(java.util.Calendar.MONTH),
                    java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH)
            );
            datePickerDialog.show();
        });
        
        btnEndDate.setOnClickListener(v -> {
            // 显示日期选择器
            android.app.DatePickerDialog datePickerDialog = new android.app.DatePickerDialog(
                    TaskRewardActivity.this,
                    (view, year, month, dayOfMonth) -> {
                        endDate[0] = String.format("%d-%02d-%02d", year, month + 1, dayOfMonth);
                        btnEndDate.setText(endDate[0]);
                    },
                    java.util.Calendar.getInstance().get(java.util.Calendar.YEAR),
                    java.util.Calendar.getInstance().get(java.util.Calendar.MONTH),
                    java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH)
            );
            datePickerDialog.show();
        });
        
        cbNoEndDate.setOnCheckedChangeListener((buttonView, isChecked) -> {
            btnEndDate.setEnabled(!isChecked);
            if (isChecked) {
                btnEndDate.setText("选择结束日期");
                endDate[0] = "";
            }
        });
        
        // 创建对话框并显示
        final androidx.appcompat.app.AlertDialog dialog = builder.create();
        
        // 设置保存按钮点击事件
        btnSave.setOnClickListener(v -> {
            // 验证输入
            if (etUsername.getText().toString().trim().isEmpty()) {
                etUsername.setError("请输入用户名");
            return;
        }
        
            // 验证比例
            try {
                if (!etLevel1Ratio.getText().toString().isEmpty()) {
                    float ratio = Float.parseFloat(etLevel1Ratio.getText().toString());
                    if (ratio < 0 || ratio > 100) {
                        etLevel1Ratio.setError("比例应在0-100之间");
                        return;
                    }
                }
                
                if (!etLevel2Ratio.getText().toString().isEmpty()) {
                    float ratio = Float.parseFloat(etLevel2Ratio.getText().toString());
                    if (ratio < 0 || ratio > 100) {
                        etLevel2Ratio.setError("比例应在0-100之间");
                        return;
                    }
                }
                
                if (!etLevel3Ratio.getText().toString().isEmpty()) {
                    float ratio = Float.parseFloat(etLevel3Ratio.getText().toString());
                    if (ratio < 0 || ratio > 100) {
                        etLevel3Ratio.setError("比例应在0-100之间");
                        return;
                    }
                }
                
                // 验证邀请奖励分成比例
                if (!etInviteLevel1Ratio.getText().toString().isEmpty()) {
                    float ratio = Float.parseFloat(etInviteLevel1Ratio.getText().toString());
                    if (ratio < 0 || ratio > 100) {
                        etInviteLevel1Ratio.setError("比例应在0-100之间");
                        return;
                    }
                }
                
                if (!etInviteLevel2Ratio.getText().toString().isEmpty()) {
                    float ratio = Float.parseFloat(etInviteLevel2Ratio.getText().toString());
                    if (ratio < 0 || ratio > 100) {
                        etInviteLevel2Ratio.setError("比例应在0-100之间");
                        return;
                    }
                }
                
                if (!etInviteLevel3Ratio.getText().toString().isEmpty()) {
                    float ratio = Float.parseFloat(etInviteLevel3Ratio.getText().toString());
                    if (ratio < 0 || ratio > 100) {
                        etInviteLevel3Ratio.setError("比例应在0-100之间");
                        return;
                    }
                }
            } catch (NumberFormatException e) {
                Toast.makeText(TaskRewardActivity.this, "请输入有效的数字", Toast.LENGTH_SHORT).show();
                return;
            }
            
            // 验证日期
            if (startDate[0].isEmpty()) {
                Toast.makeText(TaskRewardActivity.this, "请选择开始日期", Toast.LENGTH_SHORT).show();
                return;
            }
            
            if (!cbNoEndDate.isChecked() && endDate[0].isEmpty()) {
                Toast.makeText(TaskRewardActivity.this, "请选择结束日期或勾选永久有效", Toast.LENGTH_SHORT).show();
                return;
            }
            
            // 创建新的特权用户对象
            UserInfo newUser = new UserInfo();
            newUser.setUsername(etUsername.getText().toString().trim());
            newUser.setId(privilegeUsers.size() + 1);
            
            // 设置其他属性
            try {
                newUser.putExtra("level1_ratio", etLevel1Ratio.getText().toString());
                newUser.putExtra("level2_ratio", etLevel2Ratio.getText().toString());
                newUser.putExtra("level3_ratio", etLevel3Ratio.getText().toString());
                newUser.putExtra("invite_reward", etInviteReward.getText().toString());
                newUser.putExtra("invite_level1_ratio", etInviteLevel1Ratio.getText().toString());
                newUser.putExtra("invite_level2_ratio", etInviteLevel2Ratio.getText().toString());
                newUser.putExtra("invite_level3_ratio", etInviteLevel3Ratio.getText().toString());
                newUser.putExtra("start_date", startDate[0]);
                newUser.putExtra("end_date", cbNoEndDate.isChecked() ? "永久" : endDate[0]);
                newUser.putExtra("remark", etRemark.getText().toString());
            } catch (Exception e) {
                // 如果UserInfo类没有putExtra方法，则在这里添加一个自定义Map来存储这些数据
            }
            
            // 添加到列表并刷新
            privilegeUsers.add(newUser);
            privilegeUserAdapter.notifyDataSetChanged();
            
            // 保存特权用户数据
            savePrivilegeUsers();
            
            Toast.makeText(TaskRewardActivity.this, "已添加特权用户", Toast.LENGTH_SHORT).show();
            
            // 关闭对话框
            dialog.dismiss();
        });
        
        // 设置取消按钮点击事件
        btnCancel.setOnClickListener(v -> dialog.cancel());
        
        dialog.show();
    }
    
    private void removePrivilegeUser(UserInfo user) {
        privilegeUsers.remove(user);
        privilegeUserAdapter.notifyDataSetChanged();
        
        // 保存更改
        savePrivilegeUsers();
        
        Toast.makeText(this, "已移除特权用户", Toast.LENGTH_SHORT).show();
    }
    
    private void savePrivilegeUsers() {
        // 将特权用户数据保存到SharedPreferences
        SharedPreferences.Editor editor = prefs.edit();
        
        // 首先保存特权用户数量
        editor.putInt("privilege_user_count", privilegeUsers.size());
        
        // 使用JSON格式保存每个用户的数据
        com.google.gson.Gson gson = new com.google.gson.Gson();
        for (int i = 0; i < privilegeUsers.size(); i++) {
            String userJson = gson.toJson(privilegeUsers.get(i));
            editor.putString("privilege_user_" + i, userJson);
        }
        
        // 应用更改
        editor.apply();
    }

    private void saveSettings() {
        SharedPreferences.Editor editor = prefs.edit();
        
        // 保存任务奖励设置
        editor.putBoolean("task_reward_enabled", taskRewardSwitch.isChecked());
        editor.putInt("task_reward_amount", Integer.parseInt(taskRewardAmount.getText().toString()));
        editor.putInt("task_level1_percentage", Integer.parseInt(taskLevel1Percentage.getText().toString()));
        editor.putInt("task_level2_percentage", Integer.parseInt(taskLevel2Percentage.getText().toString()));
        editor.putInt("task_level3_percentage", Integer.parseInt(taskLevel3Percentage.getText().toString()));
        
        // 保存邀请奖励设置
        editor.putBoolean("invite_reward_enabled", inviteRewardSwitch.isChecked());
        editor.putInt("invite_reward_amount", Integer.parseInt(inviteRewardAmount.getText().toString()));
        
        // 保存邀请奖励分成比例设置
        editor.putInt("invite_level1_percentage", Integer.parseInt(inviteLevel1Percentage.getText().toString()));
        editor.putInt("invite_level2_percentage", Integer.parseInt(inviteLevel2Percentage.getText().toString()));
        editor.putInt("invite_level3_percentage", Integer.parseInt(inviteLevel3Percentage.getText().toString()));
        
        editor.apply();
        
        // 保存特权用户数据
        savePrivilegeUsers();
        
        Toast.makeText(this, "设置已保存", Toast.LENGTH_SHORT).show();
    }

    private void updateTaskRewardEnabled(boolean enabled) {
        taskRewardAmount.setEnabled(enabled);
        taskLevel1Percentage.setEnabled(enabled);
        taskLevel2Percentage.setEnabled(enabled);
        taskLevel3Percentage.setEnabled(enabled);
    }

    private void updateInviteRewardEnabled(boolean enabled) {
        inviteRewardAmount.setEnabled(enabled);
        inviteLevel1Percentage.setEnabled(enabled);
        inviteLevel2Percentage.setEnabled(enabled);
        inviteLevel3Percentage.setEnabled(enabled);
    }
    
    /**
     * 判断任务奖励是否启用
     * @param privilegeUser 特权用户，如果为null则检查全局设置
     * @return 是否启用任务奖励
     */
    private boolean isTaskRewardEnabled(UserInfo privilegeUser) {
        // 首先检查全局开关
        if (!taskRewardSwitch.isChecked()) {
            return false;  // 全局关闭则任务奖励不启用
        }
        
        // 全局已开启，特权用户默认启用
        return true;
    }
    
    /**
     * 判断邀请奖励是否启用
     * @param privilegeUser 特权用户，如果为null则检查全局设置
     * @return 是否启用邀请奖励
     */
    private boolean isInviteRewardEnabled(UserInfo privilegeUser) {
        // 首先检查全局开关
        if (!inviteRewardSwitch.isChecked()) {
            return false;  // 全局关闭则邀请奖励不启用
        }
        
        // 全局已开启，特权用户默认启用
        return true;
    }
    
    /**
     * 计算任务奖励
     * @param user 用户对象
     * @param level 分成级别（1-3）
     * @param taskAmount 任务金额
     * @return 计算得到的奖励金额
     */
    private float calculateTaskReward(UserInfo user, int level, float taskAmount) {
        // 检查任务奖励是否启用
        if (!isTaskRewardEnabled(user)) {
            return 0;
        }
        
        float percentage = 0;
        
        // 获取分成比例
        if (user != null) {
            // 特权用户使用自定义比例
            String ratioKey = "level" + level + "_ratio";
            String customRatio = user.getExtra(ratioKey);
            
            if (customRatio != null && !customRatio.isEmpty()) {
                try {
                    percentage = Float.parseFloat(customRatio);
                } catch (NumberFormatException e) {
                    // 使用默认比例
                    percentage = getDefaultTaskPercentage(level);
                }
            } else {
                // 没有自定义比例，使用默认比例
                percentage = getDefaultTaskPercentage(level);
            }
        } else {
            // 普通用户使用默认比例
            percentage = getDefaultTaskPercentage(level);
        }
        
        return taskAmount * percentage / 100;
    }
    
    /**
     * 获取默认的任务分成比例
     * @param level 分成级别（1-3）
     * @return 分成比例
     */
    private float getDefaultTaskPercentage(int level) {
        switch (level) {
            case 1:
                return Float.parseFloat(taskLevel1Percentage.getText().toString());
            case 2:
                return Float.parseFloat(taskLevel2Percentage.getText().toString());
            case 3:
                return Float.parseFloat(taskLevel3Percentage.getText().toString());
            default:
                return 0;
        }
    }
    
    /**
     * 计算邀请奖励
     * @param user 用户对象
     * @param level 分成级别（1-3）
     * @return 计算得到的奖励金额
     */
    private float calculateInviteReward(UserInfo user, int level) {
        // 检查邀请奖励是否启用
        if (!isInviteRewardEnabled(user)) {
            return 0;
        }
        
        float inviteAmount;
        float percentage = 0;
        
        if (user != null) {
            // 特权用户使用自定义邀请奖励
            String customInviteReward = user.getExtra("invite_reward");
            if (customInviteReward != null && !customInviteReward.isEmpty()) {
                try {
                    inviteAmount = Float.parseFloat(customInviteReward);
                } catch (NumberFormatException e) {
                    // 使用默认邀请奖励
                    inviteAmount = getDefaultInviteAmount();
                }
            } else {
                // 使用默认邀请奖励
                inviteAmount = getDefaultInviteAmount();
            }
            
            // 使用自定义分成比例
            String ratioKey = "invite_level" + level + "_ratio";
            String customRatio = user.getExtra(ratioKey);
            
            if (customRatio != null && !customRatio.isEmpty()) {
                try {
                    percentage = Float.parseFloat(customRatio);
                } catch (NumberFormatException e) {
                    // 使用默认比例
                    percentage = getDefaultInvitePercentage(level);
                }
            } else {
                // 没有自定义比例，使用默认比例
                percentage = getDefaultInvitePercentage(level);
            }
        } else {
            // 普通用户使用默认邀请奖励和比例
            inviteAmount = getDefaultInviteAmount();
            percentage = getDefaultInvitePercentage(level);
        }
        
        return inviteAmount * percentage / 100;
    }
    
    /**
     * 获取默认的邀请奖励金额
     * @return 邀请奖励金额
     */
    private float getDefaultInviteAmount() {
        return Float.parseFloat(inviteRewardAmount.getText().toString());
    }
    
    /**
     * 获取默认的邀请分成比例
     * @param level 分成级别（1-3）
     * @return 分成比例
     */
    private float getDefaultInvitePercentage(int level) {
        switch (level) {
            case 1:
                return Float.parseFloat(inviteLevel1Percentage.getText().toString());
            case 2:
                return Float.parseFloat(inviteLevel2Percentage.getText().toString());
            case 3:
                return Float.parseFloat(inviteLevel3Percentage.getText().toString());
            default:
                return 0;
        }
    }
} 